/**
 * Created by Eoly on 2016/12/31.
 */
function RunningTrack() {
    var _this = this;

    //需外部设置
    _this.centrePoint = null;

    //默认设置
    _this.speed = 2;
    _this.acceleration = 0;
    _this.direction = 0;
    _this.maxSpeed = 5;

}
RunningTrack.prototype = {
    constructor: RunningTrack,

    trackChanges: function () {
        //当需要变速曲线运动时设置函数体
    },

    trackStep: function () {
        this.trackChanges();
        if (this.speed < this.maxSpeed)
            this.speed = this.speed + this.acceleration;
        var distance = this.speed;
        this.centrePoint = this.centrePoint.getOtherPointFromDistanceAndDeg(distance, this.direction);
        return this.centrePoint;
    }

};


function BulletRunningTrack() {
    RunningTrack.call(this);
    this.speed = 2;
    this.acceleration = 0.05;
    this.direction = 0;
}


Extend(RunningTrack, BulletRunningTrack);


function EnemyRunningTrack() {
    RunningTrack.call(this);
    this.speed = 1;
    this.acceleration = 0;
    this.direction = 180;
}
Extend(RunningTrack, EnemyRunningTrack);

function EnemyFallowTrack() {
    //跟随轨迹
    RunningTrack.call(this);
    this.speed = 1;
    this.acceleration = 0.01;
    this.direction = 180;
    this.objLocked = null;
    this.sensibility = 1;
    this.followPoint = 2;
}
Extend(RunningTrack, EnemyFallowTrack);
EnemyFallowTrack.prototype.setLocked = function () {
    var objs = allObj.objs;
    var count = 0;
    do {
        var index = parseInt(objs.length * Math.random());
        count++;
    }
    while ((objs[index].playerBelongs.id != 0 || objs[index].constructor != Plane || objs[index].isDeath) && count < 20);
    if (count < 20) {
        this.objLocked = objs[index];
    }
};

EnemyFallowTrack.prototype.trackChanges = function () {
    if (this.speed >= this.followPoint) {
        if (this.objLocked == null || this.objLocked.isDeath) {
            this.setLocked();
        } else {
            var p1 = this.centrePoint;
            var p2 = this.objLocked.centrePoint;
            var deg = p1.getDegFromAnotherPoint(p2);

            if (Math.abs(deg - this.direction) < 180) {
                this.direction += deg - this.direction > 0 ? this.sensibility : -this.sensibility;
            } else {
                this.direction += deg - this.direction > 0 ? -this.sensibility : this.sensibility;
                if (this.direction < 0) {
                    this.direction += 360;
                }
                if (this.direction > 360) {
                    this.direction -= 360;
                }
            }
        }
    }

};
function EnemyNormalTrack() {
    RunningTrack.call(this);
    this.speed = 1;
    this.acceleration = 0.0001;
    this.direction = 180;
}
Extend(RunningTrack, EnemyNormalTrack);


function GivingTrack() {
    //补给包轨迹
    RunningTrack.call(this);
    this.speed = 1;
    this.acceleration = 0;
    this.direction = 180;
    this.direction_pram = true;
}
Extend(RunningTrack, GivingTrack);
GivingTrack.prototype.trackChanges = function () {
    if (this.direction < 135)
        this.direction_pram = true;
    else if (this.direction > 225)
        this.direction_pram = false;
    if (this.direction_pram)
        this.direction += 1;
    else this.direction -= 1;
};


function RandomFollowTrack() {
    //随机跟踪弹
    RunningTrack.call(this);
    this.speed = 1;
    this.acceleration = 0.01;
    this.direction = 0;
    this.direction_pram = true;
    this.sensibility = 3;
    this.followPoint = 2;


}
Extend(RunningTrack, RandomFollowTrack);
RandomFollowTrack.prototype.setLocked = function () {
    var objs = allObj.objs;
    var count = 0;
    do {
        var index = parseInt(objs.length * Math.random());
        count++;
    }
    while ((objs[index].playerBelongs.id != 1 || objs[index].isDeath) && count < 20);
    if (count < 20) {
        this.objLocked = objs[index];
    }
};
RandomFollowTrack.prototype.trackChanges = function () {
    if (this.speed >= this.followPoint) {
        if (this.objLocked == null || this.objLocked.isDeath) {
            this.setLocked();
        } else {
            var p1 = this.centrePoint;
            var p2 = this.objLocked.centrePoint;
            var deg = p1.getDegFromAnotherPoint(p2);

            if (Math.abs(deg - this.direction) < 180) {
                this.direction += deg - this.direction > 0 ? this.sensibility : -this.sensibility;
            } else {
                this.direction += deg - this.direction > 0 ? -this.sensibility : this.sensibility;
                if (this.direction < 0) {
                    this.direction += 360;
                }
                if (this.direction > 360) {
                    this.direction -= 360;
                }
            }
        }
    }
};

function DefenseTrack() {
    //补给包轨迹
    RunningTrack.call(this);
    this.speed = 3;
    this.acceleration = 0;
    this.direction = 0;
    this.r = 120;
    this.changeR = 2;
    this.nowDegToCenter = 0;
}
Extend(RunningTrack, DefenseTrack);
DefenseTrack.prototype.trackChanges = function () {
    var p1 = this.centrePoint;
    var p2 = player.plane != null ? player.plane.centrePoint : null;
    if (p2 != null) {
        var aimP = p2.getOtherPointFromDistanceAndDeg(this.r, this.nowDegToCenter);
        var distance = p1.getDistanceFromAnotherPoint(aimP);
        this.direction = p1.getDegFromAnotherPoint(aimP);
        // this.speed = Math.PI * this.r * 2 * this.changeR;
        if (distance > Math.PI * this.r * 2 * this.changeR * 2) {

        } else {
            this.nowDegToCenter += this.changeR;
        }

    }
};